package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;


    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department =
                departmentRepository.findDeptByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }


    //根据医院编号 、科室编号和工作日期，查询排班详细信息
    @Override
    public String getDeptName(String hoscode, String deptcode) {
        Department department = departmentRepository.findDeptByHoscodeAndDepcode(hoscode, deptcode);
        if(department!=null){
            return department.getDepname();
        }
        return null;
    }

    //根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //最终的封装数据
        List<DepartmentVo> result = new ArrayList<>();

        //查询所有科室
        Department department = new Department();
        department.setHoscode(hoscode);
        Example example = Example.of(department);

        //所有科室列表
        List<Department> departmentList = departmentRepository.findAll(example);

        Map<String, List<Department>> deparmentMap =
                  departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : deparmentMap.entrySet()) {
            String bigcode = entry.getKey();
            List<Department> departmentList1 = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(departmentList1.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> chil = new ArrayList<>();
            for (Department department1 : departmentList1) {
                DepartmentVo department2 = new DepartmentVo();
                department2.setDepcode(department1.getDepcode());
                department2.setDepname(department1.getDepname());
                chil.add(department2);
            }
            departmentVo.setChildren(chil);
            result.add(departmentVo);
        }


        return result;
    }


    //删除科室
    @Override
    public void remove(String hoscode, String depcode) {
        Department deptByHoscodeAndDepcode = departmentRepository.findDeptByHoscodeAndDepcode(hoscode, depcode);
        if (deptByHoscodeAndDepcode != null) {
            departmentRepository.deleteById(deptByHoscodeAndDepcode.getId());
        }
    }


    //分页
    @Override
    public Page<Department> selectPageDept(int page, int limit) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);


        //匹配器 设置查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();
        department.setHoscode("10000");
        Example<Department> example = Example.of(department, matcher);

        Page<Department> pages = departmentRepository.findAll(example, pageable);
        List<Department> content = pages.getContent();
        System.out.println("content = " + content);
        return pages;
    }


    //上传科室
    @Override
    public void saveDepartment(Map<String, Object> map) {
        //借助工具类转换为json字符串
        String jsonString = JSONObject.toJSONString(map);

        //把json字符串变成对象
        Department department = JSONObject.parseObject(jsonString, Department.class);

        Department exitDept =
                departmentRepository.findDeptByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (exitDept != null) {
            department.setId(exitDept.getId());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }
    }
}
